<template>
  <section>
    <el-row style="margin: 0 0 8px; border-bottom: 1px solid #d8d8d8">
      <el-col :span="8" :xs="24" style="background-color: #f8f8f8">
        <div class="headerContent">
          <span class="extendedHeader name" :title="showForm.name">{{
            showForm.name
          }}</span>
        </div>
      </el-col>
      <el-col :span="16" :xs="24">
        <div class="headerContent">
          <el-row class="inline-block">
            <el-button
              v-for="(btn, index) in commandData.filter(
                (item) => item.status == '1' && item.location === '2'
              )"
              :key="index"
              :type="btn.styleType"
              :icon="btn.icon"
              size="mini"
              v-preventReClick
              @click="handleClick(btn)"
              :disabled="
                btn.show != null && btn.show != undefined && btn.show != ''
                  ? !dynamicEval($data, btn.show)
                  : false
              "
              >{{ btn.name }}
            </el-button>
          </el-row>
        </div>
        <!-- <div class="headerContent ">
          <el-row class="inline-block">
            <span class="extendedHeader content">状态：</span>
            <span class="extendedHeader content">
              {{dataFormat('stateid',dataInfo.stateid)}}
            </span>
          </el-row>
          <el-row class="inline-block" v-if="$route.query.type!='projectdefine/template'">
            <span class="extendedHeader content">所有者：</span>
            <span class="extendedHeader content">
              {{dataFormat('ownerid',dataInfo.ownerid)}}
            </span>
          </el-row>
          <el-row class="inline-block">
            <span class="extendedHeader content" v-if="$route.query.type!='projectdefine/template'">
              版本：
            </span>
            <span class="extendedHeader content">
              {{dataInfo.revision}}
            </span>
          </el-row>
          <el-row class="inline-block">
            <span class="extendedHeader content">修改日期：</span>
            <span class="extendedHeader content">
              {{dataInfo.modifyTime}}
            </span>
          </el-row>
        </div> -->
      </el-col>
    </el-row>
    <el-row v-if="showFormData.length > 0" class="mt10 mb10">
      <el-col
        class="flex"
        v-for="(formItem, index) in showFormData"
        :key="index"
      >
        <template v-if="index % 2 == 0">
          <span class="extendedMain fromTitle"> {{ formItem.name }}： </span>
          <span class="extendedMain content">
            {{ dataFormat(formItem.prop, showForm[formItem.prop]) }}
          </span>
          <template v-if="index != showFormData.length - 1">
            <span class="extendedMain fromTitle">
              {{ showFormData[index + 1].name }}：
            </span>
            <span class="extendedMain content">
              {{
                dataFormat(
                  showFormData[index + 1].prop,
                  showForm[showFormData[index + 1].prop]
                )
              }}
            </span>
          </template>
          <template v-if="index == showFormData.length - 1">
            <span class="extendedMain fromTitle"></span>
            <span class="extendedMain content"></span>
          </template>
        </template>
      </el-col>
    </el-row>
    <el-row>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="审批意见" required>
          <el-input
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 6 }"
            placeholder="请输入内容"
            v-model="form.approveExplain"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="同意">
          <el-radio-group v-model="form.approve" v-if="approveList.length > 0">
            <span v-for="(radio, index) in approveList" :key="index">
              <el-radio
                v-for="(val, key) of radio"
                :key="key"
                :label="key"
                @change="handleRadioChange('reject')"
                >{{ val }}</el-radio
              >
            </span>
            >
          </el-radio-group>
          <el-radio-group v-model="form.approve" v-else>
            <el-radio label="approve" @change="handleRadioChange('reject')"
              >下一节点</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <template v-if="rejectList.length > 0">
          <el-form-item label="驳回">
            <el-radio-group v-model="form.reject">
              <span v-for="(radio, index) in rejectList" :key="index">
                <el-radio
                  v-for="(val, key) of radio"
                  :key="key"
                  :label="key"
                  @change="handleRadioChange('approve')"
                  >{{ val }}</el-radio
                >
              </span>
            </el-radio-group>
            <el-checkbox v-if="rejectSkipComplete" v-model="form.skipComplete"
              >跳过已完成节点</el-checkbox
            >
          </el-form-item>
        </template>
      </el-form>
    </el-row>
    <el-row style="padding:3px;border:1px solid #ccc;border-radius:5px;" v-if="history.length>0">
      <span style="display:inline-block;background-color:#cecece;line-height:25px;width:20%;text-indent:2em;">
      审批记录
      </span>
      <div v-for="item of history" :key="item.id" style="font-size:14px;text-indent:2em">
        <template v-if="item.approveExplain">
        <span>审批时间：{{item.accomplishTime}}</span> |
        <span>节点：{{item.nodeName}}</span> |
        <span>意见：{{item.approveExplain}}</span> |
        <span>操作：{{item.operId}}</span> |
        <span>审核者：{{dataFormat('ownerid',item.userId)}}</span>
        </template>
      </div>
    </el-row>
    <el-collapse v-model="activeNames" @change="handleChange" v-if="collapse">
      <el-collapse-item title="相关对象" name="1">
        <dynamic-table
          :list="list"
          :options="options"
          :columns="tableColumns"
          :operates="operates"
          :scroll="'fix'"
          :diff="50"
        >
        </dynamic-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
          layout="total, prev, pager, next, jumper"
        />
      </el-collapse-item>
      <el-collapse-item title="流程图" name="2">
        <flow-chart :workflowid="workflowid" />
      </el-collapse-item>
    </el-collapse>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" v-preventReClick
        >确 定</el-button
      >
      <el-button @click="cancel">取 消</el-button>
    </div>
    <!-- 添加或修改对话框 -->
    <el-dialog
      append-to-body
      ref="formTable"
      :title="title"
      :visible.sync="topen"
      width="80%"
      v-dialogDrag
      :close-on-click-modal="false"
      v-if="topen"
    >
      <dynamic-form-table
        :exsitId="exsitId"
        :geturl="geturl"
        :submiturl="addurl"
        @openTable="openTable($event)"
        @topenChanged="topenChanged($event)"
        :table="table"
        :form="subform"
      >
      </dynamic-form-table>
    </el-dialog>
  </section>
</template>

<script>
import { getConfigInfo, baseRequest } from "@/api/dynamic/dynamic";
import flowChart from "@/views/components/flowChart";
import dynamicTable from "@/views/components/dynamicTable";
import dynamicFormTable from "@/views/components/dynamicFormTable";

export default {
  name: "processReview",
  props: {
    showFormStr: {
      type: String,
      default: "review_process",
    },
    collapse: {
      type: Boolean,
      default: true,
    },
    root: null,
    parent: null,
  },
  components: {
    flowChart,
    dynamicTable,
    dynamicFormTable,
  },
  data() {
    return {
      showFormData: [],
      showForm: {},
      form: {},
      rules: {
        approveExplain: {
          required: true,
          message: "请输入审批意见",
          trigger: "blur",
        },
      },
      commandData: [],
      tableColumns: [],
      list: [],
      total: 0,
      options: {
        // table 的参数
        stripe: true, // 是否为斑马纹 table
        loading: false, // 是否添加表格loading加载动画
        highlightCurrentRow: true, // 是否支持当前行高亮显示
        mutiSelect: false, // 是否支持列表项选中功能
      },
      operates: {
        // 列操作按钮
        width: 150,
        list: [],
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      workflowid: 0,
      activeNames: [],
      approveList: [],
      rejectList: [],
      rejectSkipComplete: false,
      exsitId: [],
      subform: {},
      title: "",
      topen: false,
      table: "",
      geturl: "",
      addurl: "",
      history: [],
    };
  },
  watch: {
    root: {
      immediate: true,
      handler: function (val) {
        if (val) {
          this.getConfig(val.ids[0]);
        }
      },
    },
    parent: {
      immediate: true,
      handler: function (val) {
        if (val) {
          this.getConfig(val.taskid);
        }
      },
    },
  },
  methods: {
    getConfig(id) {
      getConfigInfo({
        showForm: this.showFormStr,
        table: "workflow_object_doc",
        command: "10001361",
      }).then((Response) => {
        this.showFormData = Response.data.showFormData;
        this.tableColumns = Response.data.tableData;
        this.commandData = Response.data.commandData;
        this.getInfo(id);
      });
    },
    getInfo(id) {
      baseRequest(
        "/pms/workflow/myWFTaskReview/" + String(id),
        "get",
        {},
        {}
      ).then((response) => {
        if (response.code == 200) {
          this.showForm = {...response.data.workflow,... response.data.task};
          this.form.id = id;
          this.$set(this.form, "approve", "approve");
          this.workflowid = response.data.workflow.id;
          this.rejectList = response.data.reject ? response.data.reject : [];
          this.approveList = response.data.approve ? response.data.approve : [];
          this.rejectSkipComplete = response.data.rejectSkipComplete;
          this.getList(this.workflowid);
        }
      });
    },
    getList(workflowid) {
      baseRequest(
        "/pms/workflow/docApproveList?projectid=" + String(workflowid),
        "get",
        {},
        {}
      ).then((response) => {
        if (response.code == 200) {
          this.list = response.rows;
          this.total = response.total;
        }
      });
      baseRequest(
        "/pms/myWorkflow/workflowRecord?workflowid=" + String(workflowid),
        "get",
        {},
        {}
      ).then((response) => {
        if (response.code == 200) {
          this.history = response.rows;
        }
      });
    },
    handleChange(val) {},
    submit() {
      if (!(this.form["approve"] || this.form["reject"])) {
        this.msgInfo("请选择审批操作");
        return false;
      }
      if (
        !(this.form["approveExplain"] && this.form["approveExplain"].trim())
      ) {
        this.msgInfo("请输入审批意见");
        return false;
      }
      // return
      baseRequest("/pms/workflow/myWFTaskAppove", "put", {}, this.form).then(
        (response) => {
          if (response.code == 200) {
            this.msgSuccess("审批成功");
            this.cancel();
            if (this.root) this.root.getMenuConfig();
            else this.refreshCurrent();
          }
        }
      );
    },
    cancel() {
      if (this.root) this.root.portaldialog.open = false;
      else this.refreshCurrent();
    },
    handleRadioChange(operation) {
      this.form[operation] = null;
      delete this.form[operation];
    },
    handleClick(btn) {
      let pageParams = JSON.parse(btn.pageParams.replace(/'/g, '"'));
      this.geturl = pageParams.getUrl;
      this.addurl = btn.url;
      this.table = pageParams.table;
      this.title = btn.description;
      this.subform = { pmsid: this.form.id, toid: this.form.id };
      this.topen = true;
    },
    topenChanged(val) {
      this.topen = false;
      if (val) {
        this.refreshCurrent();
      }
    },
  },
};
</script>
<style scoped>
.headerContent {
  float: left;
  padding: 10px 0;
  width: 100%;
}
.extendedHeader {
  display: block;
  line-height: 23px;
  white-space: nowrap;
}
.extendedHeader.name {
  font-size: 16px;
  font-weight: bold;
  font-family: "3dsregular", Arial, Helvetica, sans-serif;
  /* max-width: 250px; */
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  text-indent: 20px;
}
.extendedHeader.content {
  font-size: 13px;
  max-width: 250px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  float: left;
}
.extendedHeader.fromTitle {
  font-size: 13px;
  width: 150px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.extendedMain {
  display: block;
  line-height: 23px;
  white-space: nowrap;
  border-bottom: #d8d8d8 solid 1px;
}
.extendedMain.content {
  font-size: 13px;
  width: 300px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  padding: 5px 0 5px 10px;
}
.extendedMain.fromTitle {
  font-size: 13px;
  width: 150px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  padding: 5px 0 5px 10px;
  background-color: #f8f8f8;
}
.flex {
  display: flex;
}
.el-dialog__body {
  padding: 0 !important;
}
.dialog-footer {
  padding: 10px 0 0;
  text-align: center;
  /* border-bottom:2px solid #cecece; */
}
</style>
